package com.tangding.hprose.api.server.command.impl.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Strings;
import com.tangding.common.exception.GException;
import com.tangding.common.tools.ErrorConstants;
import com.tangding.common.utils.DateUtils;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.service.dao.role.PermissionRepository;
import com.tangding.service.dao.role.RoleRepository;
import com.tangding.service.dao.user.UserRepository;
import com.tangding.service.entity.role.Permission;
import com.tangding.service.entity.role.Role;
import com.tangding.service.entity.user.User;


/**
 * @description 获取我自己以及被我创建的用户
 *
 * @author Ranger
 * @version 2017年11月30日_下午8:25:00
 *
 */
@Component
public class GetUsers implements Command {
	static UserRepository userRepository;
	static RoleRepository roleRepository;
	static PermissionRepository permissionRepository;
	
	@Autowired
	public void setUserRepository(UserRepository userRepository) {
		GetUsers.userRepository = userRepository;
	}
	@Autowired
	public void setRoleRepository(RoleRepository roleRepository) {
		GetUsers.roleRepository = roleRepository;
	}
	@Autowired
	public void setPermissionRepository(PermissionRepository permissionRepository) {
		GetUsers.permissionRepository = permissionRepository;
	}



	@Override
	public Response exe(Object... params) throws GException {
		String uid = params[0].toString();
		
		User user = userRepository.getUserByUid(uid);
		if (user == null) {
			throw new GException(ErrorConstants.USER_NOT_FOUND);
		}
		
		List<User> users = new ArrayList<>();
		users.add(user);
		
		List<Map<String, Object>> vos = new ArrayList<>();
		
		//把自己加入vo列表
		Map<String, Object> vo = findPermissions(user);
		vos.add(vo);
		
		findChildUser(uid, users, vos);
		
		return doResponseSucc(vos);
	}
	
	
	//递归找账号
	public static void findChildUser(String pid, List<User> users) {
		Map<String, User> userMap = userRepository.getUserMap();
		userMap.values().forEach(user -> {
			if (pid != null && pid.equals(user.getPuid())) {//当前层帐号的账号
				users.add(user);
				findChildUser(user.getUid(), users);
			}
		});
	}
	
	public static void findChildUser(String pid, List<User> users, List<Map<String, Object>> vos) {
		Map<String, User> userMap = userRepository.getUserMap();
		userMap.values().forEach(user -> {
			if (pid != null && pid.equals(user.getPuid())) {//当前层帐号的自账号
				if (user.isAvailable()) {
					users.add(user);
					
					Map<String, Object> vo = findPermissions(user);
					vos.add(vo);
				}
				
				findChildUser(user.getUid(), users, vos);
			}
		});
	}
	
	public static Map<String, Object> findPermissions(User user){
		Map<String, Object> vo = new HashMap<>();
		vo.put("uid", user.getUid());
		vo.put("name", user.getName());
		vo.put("puid", Strings.isNullOrEmpty(user.getPuid()) ? "-1" : user.getPuid());
		vo.put("pname", Strings.isNullOrEmpty(user.getPname()) ? "系统" : user.getPname());
		if (user.getCreateTime() != null) {
			vo.put("createTime", DateUtils.formatDate(user.getCreateTime(), DateUtils.DATE_FORMAT));
		}
		Set<Integer> roleIds = user.getRoles();
		if (roleIds != null && roleIds.size() > 0) {
			roleIds.forEach(roleId -> {
				Role role = roleRepository.getRole(roleId);
				List<Integer> permissionIds = role.getPermission();
				if (permissionIds != null && permissionIds.size() > 0) {
					permissionIds.forEach(permissionId -> {
						Permission permission = permissionRepository.getPermission(permissionId);
						if (permission != null) {
							vo.put(permission.getKey(), "√");
						}
					});
				}
			});
		}
		
		return vo;
	}

}
